package com.example.treeHole.service.impl;

import com.example.treeHole.dao.NotificationMainMapper;
import com.example.treeHole.dao.UserInfoMapper;
import com.example.treeHole.dao.VirtualCharacterMapper;
import com.example.treeHole.service.VirtualCharacterService;
import com.example.treeHole.utils.BaseContext;
import com.example.treeHole.utils.Uuid;
import com.example.treeHole.vo.NotificationMain;
import com.example.treeHole.vo.VirtualCharacter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class VirtualCharacterServiceImpl implements VirtualCharacterService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private VirtualCharacterMapper virtualCharacterMapper;
    @Autowired
    private NotificationMainMapper notificationMainMapper;

    @Override
    public List<VirtualCharacter> findAll() {
        List<VirtualCharacter> virtualCharacterList=virtualCharacterMapper.findAll();
        return virtualCharacterList;
    }

    @Override
    public int create(VirtualCharacter virtualCharacter) {
        virtualCharacter.setId(Uuid.getUuID());
        virtualCharacter.setCreateBy(BaseContext.getCurrentId());
        virtualCharacter.setCreateDate(new Date());
        //todo 需要审核
        NotificationMain notificationMain =new NotificationMain();
        notificationMain.setId(Uuid.getUuID());
        notificationMain.setArticleId(virtualCharacter.getId());
        notificationMain.setCreateBy(BaseContext.getCurrentId());
        notificationMain.setSendId(BaseContext.getCurrentId());
        notificationMain.setCreateDate(new Date());
        //表示接受人为管理员以上人员
        notificationMain.setAcceptId("1");
        //未处理
        notificationMain.setNotificationStatus("0");
        //"5"表示虚拟任务审批
        notificationMain.setNotificationCategory("5");
        notificationMain.setNotificationType("1");
        notificationMainMapper.insert(notificationMain);
        return virtualCharacterMapper.insert(virtualCharacter);
    }

    @Override
    public VirtualCharacter findById(String id) {
        VirtualCharacter byId = virtualCharacterMapper.findById(id);
        return byId;
    }

    @Override
    public List<VirtualCharacter> usedV() {
        List<String> VIds = new ArrayList<>();
        String vu = userInfoMapper.findVUById(BaseContext.getCurrentId());
        if(!StringUtils.isEmpty(vu)) {
            String[] strVIds = vu.substring(1, vu.length() - 1).split(", ");
            for (String strVId : strVIds) {
                VIds.add(strVId);
            }
            List<VirtualCharacter> virtualCharacterList = virtualCharacterMapper.findByIds(VIds);
            return virtualCharacterList;
        }else
            return null;
    }
}





















